﻿@using static BootstrapBlazor.Shared.Samples.ListViews;
@inject IOptionsMonitor<WebsiteOptions> WebsiteOption
@inject IStringLocalizer<BootstrapBlazor.Shared.Samples.ListViews> Localizer

<ListView TItem="Product" GroupName="@(p => p.Category)" OnQueryAsync="@OnQueryAsync">
    <HeaderTemplate>
        <div>@Localizer["ProductListText"]</div>
    </HeaderTemplate>
    <BodyTemplate>
        <Card>
            <BodyTemplate>
                <img src="@context.ImageUrl" />
                <div class="listview-demo-desc">@context.Description</div>
            </BodyTemplate>
        </Card>
    </BodyTemplate>
</ListView>

@code {
    [NotNull]
    private IEnumerable<Product>? Products { get; set; }

    /// <summary>
    /// OnInitialized
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        Products = Enumerable.Range(1, 100).Select(i => new Product()
        {
            ImageUrl = $"{WebsiteOption.CurrentValue.ImageLibUrl}/images/Pic{i}.jpg",
            Description = $"Pic{i}.jpg",
            Category = $"Group{(i % 4) + 1}"
        });
    }

    private Task<QueryData<Product>> OnQueryAsync(QueryPageOptions options)
    {
        var items = Products.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems);
        return Task.FromResult(new QueryData<Product>()
        {
            Items = items,
            TotalCount = Products.Count()
        });
    }
}
